//
//  BasicInformationViewController.m
//  Estate
//
//  Created by wangdongdong on 16/7/22.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "BasicInformationViewController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "QianDTableCell.h"
#import "C_wladdCell_1.h"
#import "B_wuLiaoCell.h"
#import "TableTopStateCell.h"

@interface BasicInformationViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(strong, nonatomic)UITableView *tableView;


@end


@implementation BasicInformationViewController

- (UITableView *)tableView {
    if(_tableView==nil){
        _tableView=[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 108) style:UITableViewStyleGrouped];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        _tableView.showsVerticalScrollIndicator=NO;
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:@"QianDTableCell" bundle:nil] forCellReuseIdentifier:@"qCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"C_wladdCell_1" bundle:nil] forCellReuseIdentifier:@"doubleCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"B_wuLiaoCell" bundle:nil] forCellReuseIdentifier:@"textCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"TableTopStateCell" bundle:nil] forCellReuseIdentifier:@"firstCell"];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.tableView];
}

#pragma MARK - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section==0) {
        return 0.001;
    } else {
        return 10;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        return 7;
        
    }else if (section==1){
        return 3;
    }else if (section == 2){
            return 6;

    } else {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    long section=indexPath.section;
    long row=indexPath.row;
    if(section==0){
        if(row == 0){
            return 30;
        }
        return 44;
    }else if(section==1){
        if(row==0){
            return 44;
        }else{
            return 100;
        }
    }else if (section == 2){
        return 44;
    } else {
        return 100;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WEAKSELF
    //组
    long section = indexPath.section;
    //行
    long row = indexPath.row;
    if (section == 0) {
        if (row == 0) {
            TableTopStateCell *celld = [tableView dequeueReusableCellWithIdentifier:@"firstCell" forIndexPath:indexPath];
            celld.backgroundColor = [UIColor colorWithRed:0.7 green:0.97 blue:0.97 alpha:1.00];
            for (id obj in celld.contentView.subviews) {
                [obj removeFromSuperview];
            }
            
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake((DEF_SCREEN_WIDTH - 100)/2, 0, 100, 30)];
            [celld.contentView addSubview:btn];
            
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 8, 14, 14)];
            [btn addSubview:imgView];
            imgView.image = [UIImage imageNamed:@"chulizhong"];
            
            UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 100, 30)];
            [lab setFont:[UIFont systemFontOfSize:12]];
            [btn addSubview:lab];
            if (self.prModel.PROCESS_STAGE == PROCESS_STAGE_REVIEW) {
                [lab setText:@"状态:待审阅"];
                
            } else if (self.prModel.PROCESS_STAGE == PROCESS_STAGE_REVIEW_PASS_NO) {
                [lab setText:@"状态:审阅不通过"];
                
            } else if (self.prModel.PROCESS_STAGE == PROCESS_STAGE_CHECK) {
                [lab setText:@"状态:待复查"];
                
            } else if (self.prModel.PROCESS_STAGE == PROCESS_STAGE_CHECK_PASS_YES) {
                [lab setText:@"状态:复查通过"];
                
            } else if (self.prModel.PROCESS_STAGE == PROCESS_STAGE_END) {
                [lab setText:@"状态:结束"];
            }  else if (self.prModel.PROCESS_STAGE == PROCESS_STAGE_DOING) {
                [lab setText:@"状态:待执行"];
            }
            
            return celld;
            
        } else if (row == 1) {
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.textField.placeholder=@"";
            cell.title.text=@"项  目:";
            cell.isedit = NO;
            cell.textField.text = [LSUtility getProjectNameByProjectId:self.prModel.PRO_ID][@"PROJECT_NAME"];
            return cell;
            
        } else if (row==2) {
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qCell" forIndexPath:indexPath];
            cell.textField.placeholder=@"";
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"时  间:";
            cell.isedit = NO;
            cell.textField.text = self.prModel.CREATE_TIME;
            return cell;
            
        } else if (row==3) {
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qCell" forIndexPath:indexPath];
            cell.textField.placeholder=@"";
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"类  型:";
            cell.isedit = NO;
            
            if (self.prModel.PROBLEM_TYPE == PROBLEM_TYPE_SCOUT) {
                cell.textField.text = @"巡视";
            } else if (self.prModel.PROBLEM_TYPE == PROBLEM_TYPE_SITE) {
                cell.textField.text = @"旁站";
            } else if (self.prModel.PROBLEM_TYPE == PROBLEM_TYPE_SAFE) {
                cell.textField.text = @"安全";
            } else if (self.prModel.PROBLEM_TYPE == PROBLEM_TYPE_OTHER) {
                cell.textField.text = @"其他";
            }
            return cell;
            
        } else if(row==4) {
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qCell" forIndexPath:indexPath];
            cell.textField.placeholder=@"";
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"位  置:";
            cell.isedit = NO;
            NSDictionary *addressInfoDic = [LSUtility dictionaryWithJsonString:self.addressInfo];
            
            cell.textField.text = [NSString stringWithFormat:@"%@ %@",
                                       DEFISNUL(addressInfoDic[@"address"]),
                                       DEFISNUL(addressInfoDic[@"leftDistanceStr"])];
    
            return cell;
            
        } else if (row==5) {
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qCell" forIndexPath:indexPath];
            cell.textField.placeholder=@"";
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"发送形式";
            cell.isedit = NO;
            
            if (self.prModel.SEND_TYPE == SEND_TYPE_CONTACTLIST) {
                cell.textField.text = @"工作联系单";
            } else if (self.prModel.SEND_TYPE == SEND_TYPE_SUPERVISIONNOTICE) {
                cell.textField.text = @"监理通知单";
            } else if (self.prModel.SEND_TYPE == SEND_TYPE_STOPLIST) {
                cell.textField.text = @"工程暂停令";
            } else if (self.prModel.SEND_TYPE == SEND_TYPE_NULL) {
                cell.textField.text = @"无";
            }
            return cell;
            
        } else {
            C_wladdCell_1 *cell=[tableView dequeueReusableCellWithIdentifier:@"doubleCell" forIndexPath:indexPath];
            cell.line.hidden=YES;
            cell.leftConstraint.constant=0.;
            cell.title1.text=@"记录人:";
            cell.title2.text=@"负责人:";
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.text1.userInteractionEnabled = NO;
            cell.text2.userInteractionEnabled = NO;
            NSDictionary *dic=[self queryProcessByProcessID:self.prModel.PROCESS_ID];
            cell.text1.text = [LSUtility getuserIdByUserName:dic[@"PROCESS_PUBLISHERS"]][@"REAL_NAME"];
//            cell.text1.text = [LSUtility getuserIdByUserName:self.prModel.RECORD_USER][@"REAL_NAME"];
            cell.text2.text = [LSUtility getuserIdByUserName:self.prModel.RECORD_MASTER][@"REAL_NAME"];
            return cell;
        }
    } else if (section==1) {
        if (row==0) {
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"标  题:";
            cell.isedit = NO;
            cell.textField.text = self.prModel.PROCESS_TITLE;
            return cell;
            
        } else if(row==1) {
            B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"描  述:";
            cell.isEdit= NO;
            cell.text.text = self.prModel.PROCESS_CONTENT;
            return cell;
            
        } else {
            B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"要  求:";
            cell.isEdit= NO;
            cell.text.text = self.prModel.PROCESS_REQUIRED;
            return cell;
        }
    } else if (section == 2) {
        QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.isedit =NO;
        cell.textField.placeholder=@"";
        
        if(row==0){
            cell.title.text=@"时  限:";
            NSDictionary *dic=[self queryProcessByProcessID:self.prModel.PROCESS_ID];
            cell.textField.text = DEFISNUL(dic[@"REQUIRED_END_TIME"]);
            return cell;
            
        }else if(row==1){
            cell.title.text=@"负责单位:";
            cell.textField.text = [LSUtility getUnitNameByUnitId:self.prModel.RECIFY_UNIT][@"PRO_UNIT_NAME"];
            return cell;
            
        }else if (row==2){
            cell.title.text=@"整改人:";
            cell.textField.text = [LSUtility getuserIdByUserName:self.prModel.RECIFY_USER][@"REAL_NAME"];
            return cell;
            
        }else if(row==3){
            cell.title.text=@"严重度:";
            cell.textField.text = self.prModel.SEVERITY;
            return cell;
            
        } else if (row == 4){
            cell.title.text=@"发送时间:";
            cell.textField.text = self.beginTime;
            return cell;
            
        } else {
            cell.title.text=@"关闭时间:";
            if (self.prModel.PROCESS_STAGE == PROCESS_STAGE_REVIEW_PASS_NO || self.prModel.PROCESS_STAGE == PROCESS_STAGE_END) {
                cell.textField.text = self.endTime;
            } else {
                cell.textField.text = @"";
                cell.textField.placeholder = @"";
            }
            return cell;
        }
    } else {
        B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.title.text=@"说  明:";
        cell.isEdit= NO;
        cell.text.text = self.prModel.PROCESS_DESC;

        if (self.prModel.PROCESS_STAGE == PROCESS_STAGE_REVIEW) {
//           待审阅
            
        } else if (self.prModel.PROCESS_STAGE == PROCESS_STAGE_REVIEW_PASS_NO) {
//            审阅不通过
            
        } else if (self.prModel.PROCESS_STAGE == PROCESS_STAGE_CHECK) {
//            状态:待复查
            cell.isEdit = YES;
            
            
        } else if (self.prModel.PROCESS_STAGE == PROCESS_STAGE_CHECK_PASS_YES) {
//            状态:复查通过
            
        } else if (self.prModel.PROCESS_STAGE == PROCESS_STAGE_END) {
//            状态:结束
        }

        cell.textValueChangedBlock = ^(NSString *str) {
            weakSelf.prModel.PROCESS_DESC = str;
        };
        return cell;
    }
}

/*
 *  此方法让cell横线置顶
 */
- (void)viewDidLayoutSubviews {

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat {
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

//通过recoedId得到时限process时限
#pragma mark - 2017.3.21改 通过流程id得到流程记录信息
- (NSDictionary *)queryProcessByProcessID:(NSString *)processId{
    
    FMDatabase *db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    NSString *sqlStr=[NSString stringWithFormat:@"select * from process where id='%@'",processId];
    
    FMResultSet *result = [db executeQuery:sqlStr];
    NSDictionary *dic=[NSDictionary dictionary];
    while ([result next]) {
        dic=[result resultDictionary];
    }
    return dic;
}



@end

